﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpCmdPickFilter.h"
#include "GbmpUiPlatformCommandIds.h"


#include "IUiDocument.h"
#include "IDock.h"
#include "DisplayAndPickFilterInitalizer.h"
#include "GcmpCommandState.h"
#include "GbmpUiPlatformCommandRegister.h"
#include "PickFilterDlg.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

//////////////////////////////////////////////////////////////////////////
GbmpCmdPickFilter::GbmpCmdPickFilter(): CommandBase(ID_CMD_PICK_FILTER_GBMP)
{
}

bool GbmpCmdPickFilter::IsEnabled() const
{
    return (GcmpCommandState::IsInDocumentEnvironment() && !GcmpCommandState::IsInPickNodeReferenceMultiSelectMode());
}

bool GbmpCmdPickFilter::IsVisible() const
{
    return (GcmpCommandState::IsInDocumentEnvironment() && !GcmpCommandState::IsInPickNodeReferenceMultiSelectMode() && !GcmpCommandState::IsInAnimationEnvironment());
}

namespace
{
    const std::set<UniIdentity> CategoriestForTest = {
        BuiltInCategoryUniIdentities::BICU_STRUCTURE_WALL,
        BuiltInCategoryUniIdentities::BICU_STRUCTURE_BEAM,
        BuiltInCategoryUniIdentities::BICU_STRUCTURE_FLOOR,
        BuiltInCategoryUniIdentities::BICU_STRUCTURE_COLUMN
    };

    class TestPickFilterWidgetInitalizer : public IPickFilterWidgetInitializer
    {
    public:
        virtual std::vector<UniIdentity> GetCategories(const IDocument* pDocument) const override
        {
            return std::vector<UniIdentity>(CategoriestForTest.begin(), CategoriestForTest.end());
        }
    };
}

gcmp::OwnerPtr<IAction> GbmpCmdPickFilter::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IDockablePickFilterWidget * pPickFilterWidget = PickFilterDlg::Get();
    if (pPickFilterWidget == nullptr)
    {
        GbmpPickFilterWidgetInitalizer initalizer;
        pPickFilterWidget = PickFilterDlg::Create(initalizer);
    }
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pPickFilterWidget, L"pPickFilterWidget文档为空",L"GDMPLab",L"2024-03-30");
    IDock * pDock = pPickFilterWidget->GetOwnerDock();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDock, L"pDock文档为空",L"GDMPLab",L"2024-03-30");
    
    if (pDock->IsHidden())
    {
        pDock->Show();
        pDock->Raise();
    }
    else
    {
        pDock->Hide();
    }
    return nullptr;
}

REGISTER_GM_COMMAND(GbmpCmdPickFilter)
